$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        dataGird:null,
        init:function () {
            this.dataGrid = new xhDataGrid($('#list'),'article/getNews');
            this.dataGrid.parseToolbar().init().activeButton(['bt_list_del'],['bt_list_top']);
            this.funObj().bindDgEvent();
            this.listener();
        },
        listener:function () {
            var $this = this;

            //新增
            $('#bt_list_add').click(function () {
                $this.funObj().openAddForm();
            });

            //删除
            $('#bt_list_del').click(function () {
                $this.funObj().askDelAction();
            });

            //置顶
            $('#bt_list_top').click(function () {
                $this.funObj().changeToTop();
            });

            $('#bt_list_search').click(function () {
               $this.funObj().doSearch();
            });
            xhTool.bindDocKeyDown($('#bt_list_search'));
        },
        funObj:function () {
            var $this = this;
            return {
                openAddForm:function () {
                    window.top.xhTop.showModal({
                        title: '新增新闻',
                        content: '<iframe  src="'+baseUrl+'article/newsForm"  style="width: 100%;;border-style: none;padding: 0px;height:550px"  scrolling="yes"   ></iframe>',
                        button:'提交',
                        width:1075,
                        backClose:false
                    });
                },
                bindDgEvent:function () {
                    $this.dataGrid.loadFilter = function (data) {
                        for (var i = 0; i < data.rows.length; i++){
                            var str = '';
                            if (data.rows[i].sCoverUrl) {
                                str += '<a href="'+data.rows[i].sCoverUrl+'" target="_blank">查看PC封面</a>&nbsp;&nbsp;';
                            }
                            if (data.rows[i].sCoverMobile) {
                                str += '<a href="'+data.rows[i].sCoverMobile+'" target="_blank">查看Mobile封面</a>&nbsp;&nbsp;';
                            }
                            data.rows[i].showImage = str;
                        }
                        return data;
                    };
                    $this.dataGrid.onDblClickRow = function (rowIndex,rowData) {
                        window.top.xhTop.showModal({
                            title: '编辑新闻',
                            content: '<iframe  src="'+baseUrl+'article/newsForm?iArtId='+rowData['iArtId']+'"  style="width: 100%;;border-style: none;padding: 0px;height:550px"  scrolling="yes"   ></iframe>',
                            button:'提交',
                            width:1075,
                            backClose:false
                        });
                    }
                },
                askDelAction:function () {
                    var ids = $this.dataGrid.getSelectValues('iArtId');
                    xhExtra.askChangeStatus('article/delArticle',{ids:ids.join(',')},$this.dataGrid,'确定要删除嘛?', function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                    });
                },
                changeToTop:function () {
                    var id = $this.dataGrid.getSelectValue('iArtId');
                    xhExtra.askChangeStatus('article/changeTop',{id:id},$this.dataGrid,'确定要置顶嘛?', function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                    });
                },
                doSearch:function () {
                    $this.dataGrid.reload({
                        key:xhExtra.getValue($('#list_search'))
                    })
                }
            }
        }
    }
};